package roborally;

import Exceptions.IllegalNumberException;
import Exceptions.InsufficientEnergy;


public class Facade implements IFacade {

	/**
	 * @param args
	 * @throws IllegalNumberException 
	 */
	@SuppressWarnings("null")
	public IRobot createRobot(long x, long y, int orientation, double energy) throws IllegalNumberException {
		long[] location = null;
		location[0] = x;
		location[1]= y;
		return new Robot(energy,location,orientation);

	}
	/**
	 * get the x coordinate of the robot on the board
	 * 
	 * @param 	robot
	 */
	@Override
	public long getX(IRobot robot) {
		return robot.getPosition()[0];
	}
	/**
	 * get y coordinate of the robot on the board
	 * 
	 * @param robot
	 */
	@Override
	public long getY(IRobot robot) {
		return robot.getPosition()[1];
	}
	/**
	 * get the orientation of the robot
	 * 
	 * @param 	robot
	 * 			the robot from wich the orientation should be returned
	 */
	@Override
	public int getOrientation(IRobot robot) {
		return robot.getOrientation();
	}
	/**
	 * 
	 * @param 	robot
	 * 			gives wich robot should move																	???????????
	 * @throws 	InsufficientEnergy
	 * 			throws exception if robort has insuficient energy to move
	 */
	@Override
	public void move(IRobot robot) throws InsufficientEnergy {
		try{
			if (robot.getEnergy() < robot.getMovementEnergy())
				throw new InsufficientEnergy();
			robot.setEnergy(robot.getEnergy() - robot.getMovementEnergy());
			if (getOrientation(robot) == 0) 
				robot.setPosition(robot.getPosition()[0]-1,0);
			if (getOrientation(robot)== 1)
				robot.setPosition(robot.getPosition()[1]-1,1);
			if (getOrientation(robot) ==2)
				robot.setPosition(robot.getPosition()[0]+1,0);
			if (getOrientation(robot) == 3)
				robot.setPosition(robot.getPosition()[1]+1, 1);
		} catch (InsufficientEnergy e){
			robot.setEnergy(robot.getEnergy());
		}
	}
	/**
	 * turns the robot clockwise
	 * screenwise 0 = west(left), 1 = north(up), 2 = east(right), 3 = south(down)
	 * @param 	robot	
	 * 			the robot that has to be turned clockwise
	 * @throws 	IllegalNumberException
	 * 			throws an exception if the given robot is invalid or can't be turned
	 * 			|! ?
	 * @post	the robot is turned 90 degrees clockwise, the orientaton is upped by 1
	 * 			| new robot.getOrientaton = robot.getOrientation +1
	 */
	@Override
	public void turnClockwise(IRobot robot) {
		robot.setOrientation(robot.getOrientation() +1);
	}
	/**
	 * turns the robot counterclockwise
	 * screenwise 0 = west(left), 1 = north(up), 2 = east(right), 3 = south(down)
	 * @param 	robot
	 * 			the robot that has to be turned clockwise
	 * @throws 	IllegalNumberException
	 * 			throws exception if the robot is invalid or can't be turned
	 * 			|! ?
	 * @post	the robot is turned 90 degrees counterclockwise, the orientation is lessend by 1
	 * 			| new robot.getOrientation = robot.getOrientation -1
	 */
	@Override
	public void turnCounterClockwise(IRobot robot) {
			robot.setOrientation(robot.getOrientation() -1);
	}
	/**
	 * returns the energy of the given robot
	 * 
	 * @param 	robot
	 * 			the robot from wich the energy should be returned
	 */
	@Override
	public double getEnergy(IRobot robot) {
		return robot.getEnergy();
	}
	/**
	 * 
	 * @param 	robot
	 * 			the robot that should be recharged 
	 * @param 	energyAmount
	 * 			the amount of energy that should be recharged
	 */
	@Override
	public void recharge(IRobot robot, double energyAmount) {
		assert !((robot.getEnergy() + energyAmount) > robot.getMaxEnergy());
		robot.setEnergy(robot.getEnergy() + energyAmount);
	}
	/*
	 * nominally
	 */
	@Override
	public int isGetEnergyRequiredToReachAndMoveNextTo16Plus() {
		// TODO Auto-generated method stub
		return 0;
	}
	/*
	 * nominally
	 */
	@Override
	public double getEnergyRequiredToReach(IRobot robot, long x, long y) {
		// TODO Auto-generated method stub
		return 0;
	}
	/*
	 * defensively
	 */
	@Override
	public void moveNextTo(IRobot robot, IRobot robot2) {
		// TODO Auto-generated method stub

	}	
}
